#include "Framework.h"


#define	TEST_WAVE_FILE		"Meep.wav"

ALuint      uiBuffer;
ALuint      uiSource;  
ALint       iState;

int audioInit()
{

	// Initialize Framework
	ALFWInit();

	ALFWprintf("Initializing OpenAL\n");

	if (!ALFWInitOpenAL())
	{
		ALFWprintf("Failed to initialize OpenAL\n");
		ALFWShutdown();
		return 0;
	}

	// Generate an AL Buffer
	alGenBuffers( 1, &uiBuffer );

	// Load Wave file into OpenAL Buffer
	if (!ALFWLoadWaveToBuffer(TEST_WAVE_FILE, uiBuffer))//(char*)ALFWaddMediaPath(TEST_WAVE_FILE)
	{
		ALFWprintf("Failed to load %s\n", ALFWaddMediaPath(TEST_WAVE_FILE));
	}

	// Generate a Source to playback the Buffer
    alGenSources( 1, &uiSource );

	// Attach Source to Buffer
	alSourcei( uiSource, AL_BUFFER, uiBuffer );
	return true;
}

float	frand();

void	playSource(){
	// Play Source
	ALint playing;
	alGetSourcei(uiSource,AL_SOURCE_STATE,&playing);
	if(playing == AL_PLAYING){
		alSourceStop( uiSource );
	}
	alSourcePlay( uiSource );
	alSourcef(uiSource,AL_PITCH,(frand()*0.03f)+0.12f);
	ALFWprintf("Playing Source ");
}

int	audioShutdown(){
	do
	{
		Sleep(100);
		ALFWprintf(".");
		// Get Source State
		alGetSourcei( uiSource, AL_SOURCE_STATE, &iState);
	} while (iState == AL_PLAYING);

	ALFWprintf("\n");

	// Clean up by deleting Source(s) and Buffer(s)
	alSourceStop(uiSource);
    alDeleteSources(1, &uiSource);
	alDeleteBuffers(1, &uiBuffer);

	ALFWShutdownOpenAL();

	ALFWShutdown();

	return 0;
}
